package com.douqu.game.core.entity.world;

import com.bean.core.buffer.ByteBuffer;
import com.douqu.game.core.config.StableDataConfig;
import com.douqu.game.core.config.major.MajorConfig;
import com.douqu.game.core.e.E_StableDataType;
import com.douqu.game.core.entity.ext.data.BaseData;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.factory.WorldInfoFactory;
import com.douqu.game.core.protobuf.SGCommonProto;
import com.douqu.game.core.util.LogUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author: zhenfei
 * @Description: 世界采集信息
 * @Date: 2018-02-06 10:53
 */
public class WorldCollectData extends BaseData {

    private Map<String, WorldCollectBean> collectMap = new ConcurrentHashMap<>();
    private Map<String, WorldCollectBean> robotMap = new ConcurrentHashMap<>();
    private List<WorldCollectBean> robotList = new ArrayList<>();

    @Override
    public void init()
    {
        // 初始化采集机器人信息
        List<String> indexs = WorldInfoFactory.getInstance().collectRobotPlayerIndex;
        if (indexs != null) {
            StableDataConfig config = DataFactory.getInstance().getGameObject(DataFactory.STABLE_DATA_KEY, E_StableDataType.COLLECT_PRODUCT.getCode());
            int productNum = config.goods[0].value;
            MajorConfig majorConfig = DataFactory.getInstance().getGameObject(DataFactory.MAJOR_KEY, 1);
            for (String objectIndex : indexs) {
                WorldInfoFactory.getInstance().getWorldCollectData().addNewCollectPlayer(new WorldCollectBean(objectIndex, DataFactory.currentTime,
                        SGCommonProto.E_MAJOR_TYPE.MAJOR_TYPE_COLLECT_VALUE, 1, true, productNum, majorConfig));
            }
        } else {
            LogUtils.error("查询100个最低战力机器人错误");
        }
    }

    @Override
    public void checkInit() {
        if (robotMap.size() == 0) {
            init();
        }
    }

    @Override
    public void reset() {

    }

    @Override
    public void checkReset() {

    }

    @Override
    public void writeTo(ByteBuffer buffer) {
        buffer.writeShort(collectMap.size());
        for (Map.Entry<String, WorldCollectBean> entry : collectMap.entrySet()) {
            buffer.writeUTF(entry.getKey());
            entry.getValue().writeTo(buffer);
        }

        buffer.writeBoolean(false);
    }

    @Override
    public void loadFrom(ByteBuffer buffer) {
        int size = buffer.readShort();
        for (int i = 0; i < size; i++) {
            String key = buffer.readUTF();
            WorldCollectBean bean = new WorldCollectBean();
            bean.loadFrom(buffer);
            collectMap.put(key, bean);
        }

        buffer.readBoolean();
    }


    public WorldCollectBean getCollectPlayerByObjectIndex(String objectIndex) {
        WorldCollectBean bean = robotMap.get(objectIndex);
        if (bean != null) {
            return bean;
        }
        return collectMap.get(objectIndex);
    }

    /**
     * 添加新的采集玩家
     *
     * @param bean
     */
    public void addNewCollectPlayer(WorldCollectBean bean) {
        if (bean != null) {
            if (bean.isRobot()) {
                robotMap.put(bean.getObjectIndex(), bean);
                robotList.add(bean);
            } else {
                collectMap.put(bean.getObjectIndex(), bean);
            }
        }
    }

    public void removeByObjectIndex(String objectIndex) {
        collectMap.remove(objectIndex);
    }

    public List<WorldCollectBean> getRandomPlayers(int size, String myObjectIndex) {
        List<WorldCollectBean> list = new ArrayList<>();
        for (WorldCollectBean worldCollectBean : collectMap.values()) {
            if (worldCollectBean.getBeGrabTimes() < worldCollectBean.MAX_GRAP_TIMES) {
                if (!worldCollectBean.getObjectIndex().equals(myObjectIndex)) {
                    list.add(worldCollectBean);
                }
            }
        }

        List<WorldCollectBean> temp = new ArrayList<>();
        int playerSize = list.size();
        if (size < playerSize) {
            Collections.shuffle(list);
            temp.addAll(list.subList(0, size));
        } else if (size == playerSize) {
            temp.addAll(list);
        } else if (size > playerSize) {
            temp.addAll(list);
            int offsetNum = Math.min(size - playerSize, robotList.size());
            Collections.shuffle(robotList);
            temp.addAll(robotList.subList(0, offsetNum));
        }
        return temp;
    }
}
